package com.kgc.controller;


import com.github.pagehelper.PageInfo;
import com.kgc.entity.Brand;
import com.kgc.entity.BrandExample;
import com.kgc.service.BrandService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

@Controller
public class BrandController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private ServletContext servletContext;


    @RequestMapping("upload")
    @ResponseBody
    public Map uploadFile(MultipartFile file) throws Exception{

        String path=servletContext.getRealPath("upload");
        File dir=new File(path);
        if(!dir.exists())dir.mkdir();
        String fileName=getFileName(file.getOriginalFilename());
        String str=path+"/"+fileName;
        file.transferTo(new File(str));
        Map map=new HashMap();
        map.put("msg","文件上传成功!");
        map.put("path","upload\\"+fileName);
        return map;
    }

    private String getFileName(String fileName){
        String extName=fileName.substring(fileName.lastIndexOf("."));
        return UUID.randomUUID()+""+new Random().nextInt(1000)+extName;
    }

    @RequestMapping(value ="brand/list")
    @ResponseBody
    public Map brandlist(@RequestParam(name = "page",required = false,defaultValue = "1") int page,
                         @RequestParam(name = "limit",required = false,defaultValue = "10") int limit,
                         Brand brand){
        Map map=new HashMap();
        BrandExample brandExample=new BrandExample();
        PageInfo<Brand> pageInfo=brandService.findAll(page,limit,brand);
        map.put("code",0);
        map.put("msg","查询");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }

    @RequestMapping("brand/delete/{id}")
    @ResponseBody
    public Map delete(@PathVariable("id")int id){
        Map map=new HashMap();
        brandService.deleteByPrimaryKey(id);
        map.put("msg","删除成功");
        return map;
    }
    @RequestMapping(value = "brand/save")
    @ResponseBody
    public Map saveuser(Brand brand){
        System.out.println(brand.getLogo());
        Map map=new HashMap();
        int insert = brandService.insert(brand);
        map.put("msg","添加成功");
        map.put("data",brand);
        return map;
    }
    @RequestMapping("brand/find/{brandId}")
    @ResponseBody
    public Brand find(@PathVariable("brandId")int id){
        Brand brand =brandService.selectByPrimaryKey(id);
        return brand;
    }



    @RequestMapping("brand/update")
    @ResponseBody//将方法的返回转换成json对象  {}json对象
    public Map updateUser(Brand brand){

        Map map=new HashMap();
        brandService.updateByPrimaryKey(brand);
        map.put("msg","修改用户成功");
        map.put("data",brand);
        return map;
    }
}
